package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.part.PartDetectionResultDTO;
import com.zmn.oms.model.dto.part.PartReimburseDTO;
import com.zmn.oms.zmn.normal.business.interfaces.part.ZsNormalOrderPartBService;
import com.zmn.scm2.common.model.dio.scm.maintenance.order.use.tool.item.ScmOrderUseToolItemImgResultDIO;
import com.zmn.scm2.common.model.dro.scm.reimburse.ScmReimburseDRO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author sunlife
 * @date:2022/2/14 11:03 上午
 * description:
 */
@AliyunMQListener(topic = "${spring.mq.topic.scm2}")
@Component
public class OrderPartImageDetectionListener {

    private static final Logger logger = LoggerFactory.getLogger(OrderPartImageDetectionListener.class);

    @Autowired
    ZsNormalOrderPartBService zsNormalOrderPartBService;

    /**
     * 配件图片审核结果回调 由SCM2 发送
     * @param message
     * @return void
     * @author wangxiaokun
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.ORDER_PART_DETECTION_RESULT_CALLBACK)
    public void detectionResultCallback(String message){
        try{
            logger.info("#mq#detectionResultCallback#orderpart#recive message:{}", message);
            ScmOrderUseToolItemImgResultDIO resultDIO = JSON.parseObject(message, ScmOrderUseToolItemImgResultDIO.class);
            PartDetectionResultDTO resultDTO = BeanMapper.map(resultDIO, PartDetectionResultDTO.class);

            if (NumberUtil.isNullOrZero(resultDTO.getWorkId()) ||
                    NumberUtil.isNullOrZero(resultDTO.getMasterId()) ||
                    StringUtil.isBlank(resultDTO.getGoodsId()) ||
                    NumberUtil.isNullOrZero(resultDTO.getSourceType()) ||
                    StringUtil.isBlank(resultDTO.getReimburseId()) ||
                    NumberUtil.isNullOrZero(resultDTO.getAiStatus())) {
                throw new OmsBaseException("缺失数据，不处理");
            }
            zsNormalOrderPartBService.updatePartDetectionResult(resultDTO);

        } catch (OmsBaseException e){
            logger.error(String.format("#mq#detectionResultCallback#orderpart#error:%s", e.getMessage()), e);
        }catch (Exception e){
            logger.error(String.format("#mq#detectionResultCallback#orderpart#error:%s", e.getMessage()), e);
            e.printStackTrace();
            throw e;
        }
    }
}
